package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
@Service
@Transactional //该类中所有方法支持事务
public class ProductServiceImpl implements IProductService {
    @Autowired
    private IProductMapper mapper;
    @Override
    public void add(ProductBean productBean) {
        mapper.insert(productBean); //添加
    }

    @Override
    public void del(Integer id) {
        mapper.deleteById(id); //按id删除
    }

    @Override
    public void update(Integer id, Integer price) {
         ProductBean productBean = new ProductBean();
         productBean.setId(id);
         productBean.setPrice(price);
         mapper.updateById(productBean);
    }

    @Override
    public List<ProductBean> findAll() {
        return mapper.selectList(null);
    }

    @Override
    public ProductBean findById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public List<ProductBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (name!=null && name.length()!=0){
            qw.like("p_name",name);
        }
        if (startDate!=null){
            qw.ge("p_createDate",startDate);
        }
        if (endDate!=null){
            qw.le("p_createDate",endDate);
        }
        return mapper.selectList(qw);

    }
}
